package net.csdn.business.facade;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.domain.request.LabelOrgDto;
import net.csdn.business.common.domain.vo.LabelVo;
import net.csdn.business.common.domain.vo.migrate.MigrateInfoLogVo;
import net.csdn.business.enums.MigrateEnum;
import net.csdn.business.pojo.AtomGitLabel;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Component
public class LabelFacade {

    //private final LabelFeignClient labelFeignClient;
    private final MigrateFacade migrateFacade;


    public void addOrgLabel(LabelOrgDto labelOrgDto){
        MigrateInfoLogVo vo = migrateFacade.queryLog(MigrateEnum.MigrateInfoTypeEnum.LABEL.code, labelOrgDto.getOriginId(), labelOrgDto.getOrgPath(), "");
        if(ObjectUtils.isEmpty(vo.getId())){
            //需要 save
            //String objectId = labelFeignClient.addOrgLabel(labelOrgDto);
            String objectId =null;
            vo.setObjectId(objectId);
            vo.setStatus(MigrateEnum.Status.SUCCESS.code);
            migrateFacade.save(vo);
        }else{
            //需要 update
            labelOrgDto.setLabelId(vo.getObjectId());
            //labelFeignClient.updateOrgLabel(labelOrgDto);
            vo.setStatus(MigrateEnum.Status.SUCCESS.code);
            migrateFacade.save(vo);
        }
    }

    private List<String> queryLabelByOrg(String orgPath, List<AtomGitLabel> labelList){
        if(CollectionUtils.isEmpty(labelList)){
            return null;
        }
        Set<String> labelSet = labelList.stream().map(i -> i.getName().toLowerCase()).collect(Collectors.toSet());
        //List<LabelVo> orgLabels = labelFeignClient.getOrgLabels(orgPath,"","");
        List<LabelVo> orgLabels =new ArrayList<>();
        List<String> labelIds = orgLabels.stream().
                filter(i -> labelSet.contains(i.getLabelName().toLowerCase())).
                map(i->i.getObjectId()).
                collect(Collectors.toList());

        return labelIds;
    }
}
